package com.aidex.biz.service.impl;

import com.aidex.biz.domain.RefundOrder;
import com.aidex.biz.mapper.RefundOrderMapper;
import com.aidex.biz.service.RefundOrderService;
import com.aidex.biz.service.WxPayService;
import com.aidex.common.annotation.DataScope;
import com.aidex.common.core.domain.R;
import com.aidex.common.core.service.BaseServiceImpl;
import com.aidex.common.utils.NumberUtils;
import com.aidex.common.utils.StringUtils;
import com.github.pagehelper.PageInfo;
import com.wechat.pay.java.service.refund.model.Refund;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 售后订单Service业务层处理
 *
 * @author sean
 * @email 2074844820@qq.com
 * @date 2023-07-02
 */
@Service
@Transactional(readOnly = true)
public class RefundOrderServiceImpl extends BaseServiceImpl<RefundOrderMapper, RefundOrder> implements RefundOrderService {
    private static final Logger log = LoggerFactory.getLogger(RefundOrderServiceImpl.class);

    @Autowired
    private WxPayService wxPayService;

    /**
     * 获取单条数据
     *
     * @param refundOrder 售后订单
     * @return 售后订单
     */
    @Override
    public RefundOrder get(RefundOrder refundOrder) {
        RefundOrder dto = super.get(refundOrder);
        return dto;
    }

    /**
     * 获取单条数据
     *
     * @param id 售后订单id
     * @return 售后订单
     */
    @Override
    public RefundOrder get(String id) {
        RefundOrder dto = super.get(id);
        return dto;
    }


    /**
     * 查询售后订单列表
     *
     * @param refundOrder 售后订单
     * @return 售后订单
     */
    @Override
    @DataScope(userAlias = "a", deptAlias = "a")
    public List<RefundOrder> findList(RefundOrder refundOrder) {
        return super.findList(refundOrder);
    }

    /**
     * 分页查询售后订单列表
     *
     * @param refundOrder 售后订单
     * @return 售后订单
     */
    @Override
    @DataScope(userAlias = "a", deptAlias = "a")
    public PageInfo<RefundOrder> findPage(RefundOrder refundOrder) {
        return super.findPage(refundOrder);
    }

    /**
     * 保存售后订单
     *
     * @param refundOrder
     * @return 结果
     */
    @Override
    public boolean save(RefundOrder refundOrder) {
        return super.save(refundOrder);
    }


    /**
     * 获取最大编号
     *
     * @param refundOrder 售后订单
     * @return 结果
     */
    @Override
    public int findMaxSort(RefundOrder refundOrder) {
        return NumberUtils.nextOrder(mapper.findMaxSort(refundOrder));
    }

    /**
     * 微信退款
     *
     * @param refundOrder
     * @return
     */
    @Override
    @Transactional(readOnly = false)
    public R wxRefunds(RefundOrder refundOrder) {
        R r = new R();
        try {
            r = wxPayService.refundOrder(refundOrder);
            if (r.getCode() == 200) {
                Refund refund = (Refund) r.getData();
                refundOrder.setStatus(refund.getStatus().name());
                refundOrder.setWxRefundId(refund.getRefundId());
                refundOrder.setId(refund.getOutTradeNo());
                refundOrder.setWxRefundTime(new Date());
                refundOrder.setChannel(refund.getChannel().name());
                refundOrder.setUserReceivedAccount(refund.getUserReceivedAccount());
                refundOrder.setRemark(StringUtils.isEmpty(refundOrder.getRemark()) ? "" :
                        refundOrder.getRemark() + refund.getAmount().getTotal() / 100);
                return R.data(this.save(refundOrder));
            } else {
                return r;
            }
        } catch (Exception e) {
            log.error("退款异常:"+e.getLocalizedMessage());
            return r;
        }


    }
}
